package com.lx.huimail.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimail.R;
import com.lx.huimail.activity.p_status_act.WaitGetGoodsActivity;
import com.lx.huimail.activity.t_status_act.BackOrderDetialActivity;
import com.lx.huimail.adapter.BackGoodsAdapter;
import com.lx.huimail.adapter.OrderManagerAdapter;
import com.lx.huimail.configs.Config;
import com.lx.huimail.utils.PullToRefreshView;
import com.lx.huimail.utils.PullToRefreshView.OnHeaderRefreshListener;
import com.lx.huimail.utils.PullToRefreshView.OnFooterRefreshListener;
import com.lx.huimail.weight.ActionItem;
import com.lx.huimail.weight.TitlePopup;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;


public class BackGoodsFragment extends Fragment implements OnHeaderRefreshListener, OnFooterRefreshListener {
    private PullToRefreshView mPullToRefreshView;
    private TitlePopup titlePopup;
    private TextView iv_show_Pop;
    public static RecyclerView recyclerView;
    private EditText etSearch;
    private ImageView search;
    public static String custid="";
    private OrderManagerAdapter orderManagerAdapter;
    public static BackGoodsAdapter backGoodsAdapter;
    public static FragmentActivity instance;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_back_goods, container, false);
        instance=getActivity();
        inint(view);
        SharedPreferences sp=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        custid=sp.getString("custid","-1111");
        getBackGoodsInfo("");
        iv_show_Pop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                titlePopup.show(iv_show_Pop);

            }
        });

        return view;
    }

    private void inint(View view) {
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        iv_show_Pop=(TextView)view.findViewById(R.id.iv_show_Pop);
        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.main_pull_refresh_view);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
        etSearch=(EditText)view.findViewById(R.id.poi_et_search) ;
        search=(ImageView)view.findViewById(R.id.search) ;
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etSearch.length()==0){
                    Toast.makeText(getContext(),"请输入订单号",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent=new Intent();
                    intent.putExtra("orderno",etSearch.getText()+"");
                    intent.putExtra("status","12321343");
                    intent.setClass(getContext(), BackOrderDetialActivity.class);
                    startActivity(intent);
                }

            }
        });
        // 实例化标题栏弹窗
        titlePopup = new TitlePopup(getContext(), ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        titlePopup.setItemOnClickListener(onitemClick);
        // 给标题栏弹窗添加子类
        titlePopup.addAction(new ActionItem(getContext(), R.string.waittuihuo));
        titlePopup.addAction(new ActionItem(getContext(), R.string.tuihuoing));
        titlePopup.addAction(new ActionItem(getContext(), R.string.wancheng));
    }

    private TitlePopup.OnItemOnClickListener onitemClick = new TitlePopup.OnItemOnClickListener() {

        @Override
        public void onItemClick(ActionItem item, int position) {
            switch (position) {
                case 0:// 未配送
                    getBackGoodsInfo("0");
                    break;
                case 1:// 配送完成
                    getBackGoodsInfo("2");
                    break;
                case 2://配送中
                    getBackGoodsInfo("1");
                    break;
            }
        }
    };

    /**
     * 获得退货信息
     */
    public static void getBackGoodsInfo(String s){
        HashMap<String,String> map=new HashMap<>();
        map.put("page","1");
        map.put("rows","1000000");
        map.put("sendlinkerid",custid);
        map.put("sendstatus",s);
        Gson gson=new Gson();
        String parmas=gson.toJson(map);
        OkHttpUtils
                .post()
                .url(Config.url+"send/searchExiteOrder.do")
                .addParams("data",parmas)
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("退货单=="+response);
                        ArrayList<HashMap<String,String>> mList=new ArrayList<HashMap<String, String>>();
                        JSONArray jsonArray=null;
                        try {
                            jsonArray=new JSONArray(response);
                            for(int i=0;i<jsonArray.length();i++){
                                HashMap<String,String> map=new HashMap<String, String>();
                                map.put("orderno",jsonArray.getJSONObject(i).getString("orderno"));
                                map.put("exiteno",jsonArray.getJSONObject(i).getString("exiteno"));
                                map.put("createtime",jsonArray.getJSONObject(i).getString("createtime"));
                                map.put("sendstatus",jsonArray.getJSONObject(i).getString("sendstatus"));
                                map.put("exitecount",jsonArray.getJSONObject(i).getString("exitecount"));
                                map.put("exitemoney",jsonArray.getJSONObject(i).getString("exitmoney"));
                                map.put("custname",jsonArray.getJSONObject(i).getString("custname"));
                                map.put("exiteaddress",jsonArray.getJSONObject(i).getString("exiteaddress"));
                                map.put("isgolds",jsonArray.getJSONObject(i).getString("isgolds"));
                                map.put("id",jsonArray.getJSONObject(i).getString("id"));
                                map.put("name",jsonArray.getJSONObject(i).getString("name"));
                                map.put("array",String.valueOf(jsonArray.getJSONObject(i).getJSONArray("prolist")));
                                mList.add(map);

                            }
                            backGoodsAdapter=new BackGoodsAdapter(instance,mList);
                            recyclerView.setLayoutManager(new LinearLayoutManager(instance));
                            recyclerView.setAdapter(backGoodsAdapter);
                            backGoodsAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);

                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 2000);
        getBackGoodsInfo("");
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 2000);
        getBackGoodsInfo("");

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }
    };
}